package com.wan.commonsdk;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.sourceforge.simcpux.Util;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

//------------------------------------------------------------------------
//
// 后端通信类，�?51WAN后台交换数据
//
//------------------------------------------------------------------------

public class HttpUtils {
	private static final int CONNECTION_TIMEOUT = 15000;
	private static final int SO_TIMEOUT = 20000;

	public static HttpClient initHttpClient() {
		return null;

	}

	public static String HttpPayAcounts() {

		// ?g=Api&m=Pageapi&a=gamealipayinfo&platformgid=&sign=?
		String httpurl = "http://mmanager.51wan.com/index.php?g=Api&m=Pageapi&a=gamealipayinfo&platformgid=";
		String acountInfo = "";
		if (!isWork()) {
			return null;
		} else {
			try {
				// HttpPost httpRequest = new HttpPost(httpurl);
				//
				// List<NameValuePair> params = new ArrayList<NameValuePair>();
				//
				// params.add(new BasicNameValuePair("g", "Api"));
				// params.add(new BasicNameValuePair("m", "Pageapi"));
				// params.add(new BasicNameValuePair("a", "gamealipayinfo"));
				//
				// params.add(new BasicNameValuePair("platformgid",
				// Conet.gameID+""));
				String sign = MD5Utils.getMD5("xy51wan" + Conet.gameID);
				// sign.substring(6, 21)
				httpurl = httpurl + Conet.gameID + "&sign=" + sign.substring(5, 20);
				HttpGet httpRequest = new HttpGet(httpurl);
				// params.add(new BasicNameValuePair("sign",));
				// httpRequest.setEntity(new UrlEncodedFormEntity(params,
				// HTTP.UTF_8));
				DefaultHttpClient httpclient = new DefaultHttpClient();
				HttpResponse httpResponse = httpclient.execute(httpRequest);
				if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
					StringBuilder builder = new StringBuilder();
					BufferedReader bufferedReader2 = new BufferedReader(
							new InputStreamReader(httpResponse.getEntity()
									.getContent()));
					for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2
							.readLine()) {
						builder.append(s);
					}
					String a = builder.toString();
					acountInfo = a;

					acountInfo = acountInfo.substring(1,
							acountInfo.length() - 2);
					JSONObject jsonObject = new JSONObject(acountInfo);
					Keys.NEW_SELLER = jsonObject.optString("aliaccount");
//					paysh@51wan.com								 
					Keys.NEW_SELLER = Keys.NEW_SELLER.substring(0, 2)+Keys.NEW_SELLER.substring(3, 4)+Keys.NEW_SELLER.substring(5);
					Keys.NEW_PARTNER = jsonObject.optString("publickey");
					Keys.NEW_PARTNER = Keys.NEW_PARTNER.substring(0, 2)+ Keys.NEW_PARTNER.substring(3, 6)+Keys.NEW_PARTNER.substring(7);
//					208889211315172262
//					208891315172262
					Keys.NEW_PRIVATE = jsonObject.optString("secretkey");
					Keys.NEW_PRIVATE = Keys.NEW_PRIVATE.substring(0, 5)+Keys.NEW_PRIVATE.substring(6, 33)+Keys.NEW_PRIVATE.substring(34, 56)+Keys.NEW_PRIVATE.substring(57);
					
					
					
					Conet.ALIPAY_URL = jsonObject.optString("tokenurl");
					Conet.ALIPAY_URL = Conet.ALIPAY_URL.substring(0, 10) + Conet.ALIPAY_URL.substring(14, 25)+Conet.ALIPAY_URL.substring(28);
//					Conet.ALIPAY_URL.charAt(2);
					System.out.print(Conet.ALIPAY_URL);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return acountInfo;
	}

	/**
	 * 
	 * @param android_id
	 * @return
	 */

	public static WanAppInfo HttpInit(String gameID) {
		String httpUrl = Conet.INIT_URL + gameID;
		Log.d("51wan SDK", "gameID == " + gameID);
		if (!isWork()) {
			return null;
		} else {
			HttpGet httpRequest = new HttpGet(httpUrl);
			try {
				httpRequest
						.setHeader("User-Agent",
								"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:0.9.4)");
				HttpResponse httpResponse = new DefaultHttpClient()
						.execute(httpRequest);
				if (httpResponse.getStatusLine().getStatusCode() == 200) {
					String strResult = EntityUtils.toString(
							httpResponse.getEntity(), "UTF-8");
					String result = strResult.substring(1,
							strResult.length() - 2);
					JSONObject jsonObject = new JSONObject(result);

					// ({"appid":"12","appkey":"lyb","appname":"\u7405\u740a\u699c"
					// ,"adid":"45","information":"\u597d\u73a9\u7684\u6e38\u620f "});
					WanAppInfo wanappInfo = new WanAppInfo();
					wanappInfo.setAppId(jsonObject.optString("appid"));
					wanappInfo.setAppSecret(jsonObject.optString("appkey"));
					wanappInfo.setGamename(jsonObject.optString("appname"));
					wanappInfo.setAdId(jsonObject.optString("adid"));
					wanappInfo.setInformation(jsonObject
							.optString("information"));
					wanappInfo.setPlatformgid(jsonObject
							.optString("platformgid"));
					wanappInfo.setCzbl(jsonObject.optString("proportion"));
					wanappInfo.setGameName(jsonObject.optString("name"));
					
					
					
					Log.d("51wan  MT debug","51wan MT init http success");
					return wanappInfo;
				}
			} catch (Exception e) {
				Log.d("51wan  MT debug","51wan MT init http fail ");
				e.printStackTrace();
			}
		}
		return null;

	}

	// ------------------------------------------------------------------------
	//
	// �?活接�?
	//
	// ------------------------------------------------------------------------
	public static String HttpActive(String android_id) {
		String httpUrl = Conet.Active_URL;
		if (!isWork()) {
			return null;
		} else {
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("a", Conet.mWanAppInfo.getAdId()));
			params.add(new BasicNameValuePair("b", android_id));
			params.add(new BasicNameValuePair("c", String
					.valueOf(Conet.mWanAppInfo.getPlatformgid())));
			params.add(new BasicNameValuePair("whotype", WanCommonSdk.WHO_TYPE));
			HashMap<String, String> result = HttpExecute(httpUrl, params, 0);
		}
		return "ok";

	}

	// ------------------------------------------------------------------------
	//
	// 登录接口
	//
	// ------------------------------------------------------------------------
	public static HashMap<String, String> HttpLogin(String username,
			String password) {
		if (!isWork()) {
			return null;
		} else {
			String httpUrl = Conet.LOGIN_URL;
			String game = Conet.mWanAppInfo.getGamename();
			String sign = MD5Utils.getMD5(username + password + game);

			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("username", username));
			params.add(new BasicNameValuePair("password", password));
			params.add(new BasicNameValuePair("game", game));
			params.add(new BasicNameValuePair("sign", sign));
			params.add(new BasicNameValuePair("fromwhere",
					WanCommonSdk.FROMWHERE));
			params.add(new BasicNameValuePair("gameid", String
					.valueOf(Conet.mWanAppInfo.getPlatformgid())));
			params.add(new BasicNameValuePair("serverid", String
					.valueOf(Conet.serviceID)));
			return HttpExecute(httpUrl, params, 0);
		}
	}

	// ------------------------------------------------------------------------
	//
	// 发�?�验证码接口
	//
	// ------------------------------------------------------------------------
	public static HashMap<String, String> HttpSendCode(String userid,
			String type, String phone) {
		if (!isWork()) {
			return null;
		} else {
			String httpUrl = Conet.SEND_PHONE_CODE_URL;
			String client_id = Conet.mWanAppInfo.getAppId();
			String sign = MD5Utils.getMD5(userid + type
					+ Conet.mWanAppInfo.getAppSecret());
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			Util.LogUtil("client_id:" + client_id);
			params.add(new BasicNameValuePair("client_id", client_id));
			params.add(new BasicNameValuePair("userid", userid));
			params.add(new BasicNameValuePair("type", type));
			params.add(new BasicNameValuePair("phone", phone));
			params.add(new BasicNameValuePair("sign", sign));
			return HttpExecute(httpUrl, params, 0);
		}
	}

	// ------------------------------------------------------------------------
	//
	// 获取用户ID接口
	//
	// ------------------------------------------------------------------------
	public static HashMap<String, String> HttpGetUserid(String username) {
		String httpUrl = Conet.USER_INFO_URL;
		String sign = MD5Utils.getMD5(
				username + "51wanfjkadsjfkj34kjtkljkf4jrkqjzyw").substring(3,
				19);
		if (!isWork()) {
			return null;
		} else {
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("username", username));
			params.add(new BasicNameValuePair("sign", sign));
			return HttpExecute(httpUrl, params, 1);
		}
	}

	// ------------------------------------------------------------------------
	//
	// 获取用户名称
	//
	// ------------------------------------------------------------------------
	public static HashMap<String, String> HttpGetuserName(String userid) {
		String httpUrl = Conet.USER_INFO_URL;
		String sign = MD5Utils.getMD5(
				userid + "51wanfjkadsjfkj34kjtkljkf4jrkqjzyw").substring(3, 19);
		if (!isWork()) {
			return null;
		} else {
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("userid", userid));
			params.add(new BasicNameValuePair("sign", sign));
			return HttpExecute(httpUrl, params, 1);
		}
	}

	// ------------------------------------------------------------------------
	//
	// 绑定手机接口
	//
	// ------------------------------------------------------------------------
	public static HashMap<String, String> HttpBindMobile(String userid,
			String type, String code, String phone) {
		if (!isWork()) {
			return null;
		} else {
			String httpUrl = Conet.BIND_MOBILE_URL;
			String client_id = Conet.mWanAppInfo.getAppId();
			String sign = MD5Utils.getMD5(userid + code
					+ Conet.mWanAppInfo.getAppSecret());
			Util.LogUtil("content:==" + code + " phone:" + phone + " userid:"
					+ userid + " client_id:" + client_id + " sign:" + sign
					+ " type:" + type);
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("client_id", client_id));
			params.add(new BasicNameValuePair("userid", userid));
			params.add(new BasicNameValuePair("code", code));
			params.add(new BasicNameValuePair("type", type));
			params.add(new BasicNameValuePair("phone", phone));
			params.add(new BasicNameValuePair("sign", sign));
			return HttpExecute(httpUrl, params, 0);
		}
	}

	// ------------------------------------------------------------------------
	//
	// 游客登录接口
	//
	// ------------------------------------------------------------------------
	public static HashMap<String, String> HttpGuestLogin() {
		String httpUrl = Conet.Guest_RES_RUL;
		String sign = MD5Utils.getMD5(Conet.mWanAppInfo.getAppSecret()
				+ Conet.imei);
		if (!isWork()) {
			return null;
		} else {
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			Util.LogUtil("HttpGuestLogin---------");
			params.add(new BasicNameValuePair("client_id", Conet.mWanAppInfo
					.getAppId()));
			params.add(new BasicNameValuePair("sign", sign));
			params.add(new BasicNameValuePair("Android", Conet.imei));
			params.add(new BasicNameValuePair("who", Conet.who));
			params.add(new BasicNameValuePair("whotype", Conet.who_type));
			return HttpExecute(httpUrl, params, 2);
		}
	}

	// ------------------------------------------------------------------------
	//
	// 用户注册接口
	//
	// ------------------------------------------------------------------------
	public static HashMap<String, String> HttpReg(String username,
			String password) {
		String httpUrl = Conet.GAME_REG_URL;
		String sign = MD5Utils.getMD5(username + password
				+ Conet.mWanAppInfo.getAppSecret());
		if (!isWork()) {
			return null;
		} else {
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("username", username));
			params.add(new BasicNameValuePair("password", password));
			params.add(new BasicNameValuePair("client_id", Conet.mWanAppInfo
					.getAppId()));
			params.add(new BasicNameValuePair("sign", sign));
			params.add(new BasicNameValuePair("who", Conet.who));
			params.add(new BasicNameValuePair("whotype", Conet.who_type));
			// if (Util.getAdID() != null && !"0".equals(Util.getAdID())) {
			// params.add(new BasicNameValuePair("who", Util.getAdID()));
			// params.add(new BasicNameValuePair("whotype", "6"));
			// }
			/*params.add(new BasicNameValuePair("gameid", String
					.valueOf(Conet.mWanAppInfo.getPlatformgid())));*/

			return HttpExecute(httpUrl, params, 0);
		}
	}

	// ------------------------------------------------------------------------
	//
	// 修改密码接口
	//
	// ------------------------------------------------------------------------
	public static HashMap<String, String> HttpUpdatePasswrd(String username,
			String password, String passwordNew, String userid, String type,
			String phone) {
		if (!isWork()) {
			return null;
		} else {
			String httpUrl = Conet.UPDATE_PASSWORD_URL;
			String sign = "";
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("userid", userid));
			params.add(new BasicNameValuePair("ischekUser", "1"));
			params.add(new BasicNameValuePair("username", username));
			params.add(new BasicNameValuePair("change_type", type));
			params.add(new BasicNameValuePair("password", password));
			if (type == "1") {
				params.add(new BasicNameValuePair("o_password", password));
				params.add(new BasicNameValuePair("password", passwordNew));
				sign = MD5Utils.getMD5(username + passwordNew + userid
						+ Conet.mWanAppInfo.getAppSecret());
			} else if (type == "2") {
				params.add(new BasicNameValuePair("code", passwordNew));
				params.add(new BasicNameValuePair("phone", phone));
				sign = MD5Utils.getMD5(username + password + userid
						+ Conet.mWanAppInfo.getAppSecret());
			}

			params.add(new BasicNameValuePair("client_id", Conet.mWanAppInfo
					.getAppId()));
			params.add(new BasicNameValuePair("sign", sign));
			params.add(new BasicNameValuePair("who", Util.getWho_ADID()));
			params.add(new BasicNameValuePair("whotype", WanCommonSdk.WHO_TYPE));
			params.add(new BasicNameValuePair("gameid", String
					.valueOf(Conet.mWanAppInfo.getPlatformgid())));

			return HttpExecute(httpUrl, params, 0);
		}
	}

	// ------------------------------------------------------------------------
	//
	// 游客绑定接口
	//
	// ------------------------------------------------------------------------
	public static HashMap<String, String> HttpGuestReg(String username,
			String password, String userid) {
		String httpUrl = Conet.UPDATE_PASSWORD_URL;
		String sign = MD5Utils.getMD5(username + password + userid
				+ Conet.mWanAppInfo.getAppSecret());
		if (!isWork()) {
			return null;
		} else {
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("userid", userid));
			params.add(new BasicNameValuePair("username", username));
			params.add(new BasicNameValuePair("password", password));
			params.add(new BasicNameValuePair("client_id", Conet.mWanAppInfo
					.getAppId()));
			params.add(new BasicNameValuePair("sign", sign));
			String whotype = "3";
			if (Integer.parseInt(Conet.who) > 50000) {
				whotype = "6";
			}
			params.add(new BasicNameValuePair("who", Conet.who));
			params.add(new BasicNameValuePair("whotype", whotype));

			/*
			 * params.add(new BasicNameValuePair("who", Util.getWho_ADID()));
			 * params.add(new BasicNameValuePair("whotype",
			 * WanCommonSdk.WHO_TYPE));
			 */
			params.add(new BasicNameValuePair("gameid", String
					.valueOf(Conet.mWanAppInfo.getPlatformgid())));
			return HttpExecute(httpUrl, params, 0);
		}
	}

	// ------------------------------------------------------------------------
	//
	// HTTP访问接口
	//
	// ------------------------------------------------------------------------
	public static HashMap<String, String> HttpExecute(String url,
			List<NameValuePair> params, int type) {
		HashMap<String, String> result = new HashMap<String, String>();
		HttpPost httpRequest = new HttpPost(url);
		Util.LogUtil("HttpExecute---------" + url);
		DefaultHttpClient httpclient = new DefaultHttpClient();
		// httpclient.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT,
		// SO_TIMEOUT);
		// httpclient.getParams().setIntParameter(
		// HttpConnectionParams.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);
		try {
			Util.LogUtil("HttpExecute-----conn----");
			httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			Util.LogUtil("HttpExecute-----execute----");
			HttpResponse httpResponse = httpclient.execute(httpRequest);
			Util.LogUtil("content:" + params.toString());
			Util.LogUtil("HttpExecute-----getStatusCode----"
					+ httpResponse.getStatusLine().getStatusCode());
			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				try {
					StringBuilder builder = new StringBuilder();
					BufferedReader bufferedReader2 = new BufferedReader(
							new InputStreamReader(httpResponse.getEntity()
									.getContent()));
					for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2
							.readLine()) {
						builder.append(s);
					}
					JSONObject jsonObject = new JSONObject(builder.toString());
					Util.LogUtil("HttpExecute-----result----" + jsonObject);
					String status = (String) jsonObject.get("code");
					if (Integer.valueOf(status) == WanServerResposeCode.WAN_LOGIN_SUCCESS) {
						JSONObject jsondata = (JSONObject) jsonObject
								.get("data");
						String user_id = (String) jsondata.get("userid");
						result.put("user_id", user_id);
						if (type == 1) {
							String email = (String) jsondata.get("email");
							result.put("email", email);
						} else if (type == 0) {
							String access_token = (String) jsondata
									.get("token");
							result.put("access_token", access_token);
						} else if (type == 2) {
							String username = (String) jsondata.get("username");
							String access_token = (String) jsondata
									.get("token");
							result.put("username", username);
							result.put("access_token", access_token);
						}

					}
					result.put("status", status);

				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ConnectTimeoutException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			result = null;
			e.printStackTrace();
		}
		return result;
	}

	// ------------------------------------------------------------------------
	//
	// 51WAN订单创建接口
	//
	// ------------------------------------------------------------------------
	public static String HttpGetOrder(PayInfo payinfo) {
		String order_id = "";
		if (!isWork()) {
			return null;
		} else {
			HashMap<String, String> result = new HashMap<String, String>();
			String httpUrl = Conet.PAY_ORDER_URL;
			String paymoney = payinfo.getFixedmoney();
			String money = paymoney;
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			if (payinfo.getYhqInfo() != null
					&& Double.parseDouble(payinfo.getYhqInfo().getPrice()) >= 1) {

				params.add(new BasicNameValuePair("giftcodeid", payinfo
						.getYhqInfo().getId()));
				params.add(new BasicNameValuePair("giftcode", payinfo
						.getYhqInfo().getGiftcode()));
				params.add(new BasicNameValuePair("giftmoney", payinfo
						.getYhqInfo().getPrice()));
				money = (int) (Integer.parseInt(paymoney) - Double
						.parseDouble(payinfo.getYhqInfo().getPrice())) + "";
			}
			String gamemoney = Integer.parseInt(paymoney) * Integer.parseInt(payinfo.getCzbl()) + "";
			String sign = payinfo.getSign(Conet.mUserInfo.getUser_name(),
					gamemoney, paymoney);
			HttpPost httpRequest = new HttpPost(httpUrl);
			DefaultHttpClient httpclient = new DefaultHttpClient();
			httpclient.getParams().setIntParameter(
					HttpConnectionParams.SO_TIMEOUT, SO_TIMEOUT);
			httpclient.getParams()
					.setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT,
							CONNECTION_TIMEOUT);// ���ӳ�ʱ

			params.add(new BasicNameValuePair("userid", Conet.mUserInfo
					.getUser_id()));
			params.add(new BasicNameValuePair("username", Conet.mUserInfo
					.getUser_name()));
			params.add(new BasicNameValuePair("gameid", Conet.mWanAppInfo
					.getPlatformgid() + ""));
			params.add(new BasicNameValuePair("gamename", payinfo.getGameabbr()));
			params.add(new BasicNameValuePair("spid", payinfo.getServer()));
			params.add(new BasicNameValuePair("paymoney", payinfo
					.getFixedmoney()));
			params.add(new BasicNameValuePair("gamemoney", gamemoney));
			params.add(new BasicNameValuePair("tradeid", payinfo.getTradeid()));
			params.add(new BasicNameValuePair("itemid", payinfo.getItemid()));
			params.add(new BasicNameValuePair("money", money));
			params.add(new BasicNameValuePair("gameName", Conet.mWanAppInfo
					.getGameName()));
			params.add(new BasicNameValuePair("tradename", payinfo
					.getTradename()));
			params.add(new BasicNameValuePair("itemname", payinfo.getItemname()));
			params.add(new BasicNameValuePair("extrastr", payinfo.getExtrastr()));

			// params.add(new
			// BasicNameValuePair("testserver",payinfo.getTestserver()));
			// 213#@#ASDsds87332789hIooNm23424fsfnfggslkj(*jfksfjiKHJOogbnrioadsfkjbX
			params.add(new BasicNameValuePair("sign", sign));
			try {
				httpRequest.setEntity(new UrlEncodedFormEntity(params,
						HTTP.UTF_8));
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
				HttpResponse httpResponse = httpclient.execute(httpRequest);
				if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
					try {
						StringBuilder builder = new StringBuilder();
						BufferedReader bufferedReader2 = new BufferedReader(
								new InputStreamReader(httpResponse.getEntity()
										.getContent()));
						for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2
								.readLine()) {
							builder.append(s);
						}
						JSONObject jsonObject = new JSONObject(
								builder.toString());
						String status = (String) jsonObject.get("status");
						Log.d("bac","order === "+status);
						if (status.equals("0")) {
							order_id = jsonObject.optString("orderid");
						}
						return order_id;
						// Message msg = mHandler.obtainMessage();
						// msg.obj = order_id;
						// if(payinfo.getTradeid().equals("19")){
						// msg.what = 19;
						//
						// }else if(payinfo.getTradeid().equals("1012")){
						// msg.what = 1012;
						// }
						// mHandler.sendMessage(msg);
						// if(Integer.valueOf(status) ==
						// WanServerResposeCode.WAN_GETORDER_SUCCESS)
						// {
						//
						// String order_id = (String) jsonObject.get("orderid");
						// payinfo.setTradeno(order_id);
						// result.put("orderid", order_id);
						// }
						// Log.i("订单状�?�码", status);
						// result.put("status", status);
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} else {
					// Log.i("真是失败�?", "是失败了");
				}
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ConnectTimeoutException e) {
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String ss = order_id;

			return ss;
		}
	}

	// ------------------------------------------------------------------------
	//
	// 成功支付后请�?51WAN服务�?
	//
	// ------------------------------------------------------------------------
	public static HashMap<String, String> HttpExecuteOrder(String orderid,
			String userid, String gamename, String key) {
		HashMap<String, String> result = new HashMap<String, String>();
		String httpUrl = Conet.PAY_ORDER_URL;
		String need_sign = "key=" + key + "&" + orderid + "&" + userid + "&"
				+ gamename;
		String sign = MD5Utils.getMD5(need_sign);
		HttpPost httpRequest = new HttpPost(httpUrl);
		DefaultHttpClient httpclient = new DefaultHttpClient();
		httpclient.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT,
				SO_TIMEOUT);
		httpclient.getParams().setIntParameter(
				HttpConnectionParams.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);// ���ӳ�ʱ
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("orderid", orderid));
		params.add(new BasicNameValuePair("userid", userid));
		params.add(new BasicNameValuePair("subject", gamename));
		params.add(new BasicNameValuePair("sign", sign));
		try {
			httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			HttpResponse httpResponse = httpclient.execute(httpRequest);
			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				try {
					StringBuilder builder = new StringBuilder();
					BufferedReader bufferedReader2 = new BufferedReader(
							new InputStreamReader(httpResponse.getEntity()
									.getContent()));
					for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2
							.readLine()) {
						builder.append(s);
					}
					JSONObject jsonObject = new JSONObject(builder.toString());
					String status = (String) jsonObject.get("status");
					if (Integer.valueOf(status) == WanServerResposeCode.WAN_GETORDER_SUCCESS) {

						String order_id = (String) jsonObject.get("orderid");
						result.put("orderid", order_id);
					}
					result.put("status", status);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ConnectTimeoutException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	public static int isGuest(String userid, String username) {
		StringBuilder builder = new StringBuilder();
		String httpUrl = Conet.Guest_LOGIN_CC;
		int isguest = 0;
		HttpPost httpRequest = new HttpPost(httpUrl);
		DefaultHttpClient httpclient = new DefaultHttpClient();
		httpclient.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT,
				SO_TIMEOUT);
		httpclient.getParams().setIntParameter(
				HttpConnectionParams.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);// ���ӳ�ʱ
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("userid", userid));
		params.add(new BasicNameValuePair("username", username));
		try {
			httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
			HttpResponse httpResponse = httpclient.execute(httpRequest);
			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

				BufferedReader bufferedReader2 = new BufferedReader(
						new InputStreamReader(httpResponse.getEntity()
								.getContent()));
				for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2
						.readLine()) {
					builder.append(s);
				}
				isguest = Integer.parseInt(builder.toString());
			}
		} catch (Exception e) {

		}
		return isguest;
	}

	public static boolean isWork() {
		Context context = Conet.mActivity.getApplicationContext();
		// 获取手机所有连接管理对象（包括对wi-fi,net等连接的管理）
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		// 获取NetworkInfo对象
		NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
		for (int i = 0; i < networkInfo.length; i++) {
			if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {

				return true;
			}

		}
		return false;
	}
}
